package com.xgimi.open.skill.datamanager.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.xgimi.open.skill.datamanager.domain.entity.IntentDO;
import com.xgimi.open.skill.datamanager.domain.mapper.IntentMapper;
import com.xgimi.open.skill.datamanager.domain.service.IntentService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author coderzpw.zhang
 * @description 针对表【intent(意图表)】的数据库操作Service实现
 * @createDate 2023-02-07 19:23:48
 */
@Service
public class IntentServiceImpl extends ServiceImpl<IntentMapper, IntentDO>
        implements IntentService {

    @Override
    public List<IntentDO> queryIntentByNameAndVid(IntentDO intentDO) {
        LambdaQueryWrapper<IntentDO> wrapper = new LambdaQueryWrapper();
        wrapper.eq(!StringUtils.isEmpty(intentDO.getIntentName()), IntentDO::getIntentName, intentDO.getIntentName());
        wrapper.eq(!Objects.isNull(intentDO.getSkillVid()), IntentDO::getSkillVid, intentDO.getSkillVid());
        return this.list(wrapper);
    }

    @Override
    public Boolean saveIntent(IntentDO intentDO) {
        return this.save(intentDO);
    }

    @Override
    public List<IntentDO> listBySkillVid(Long skillVid) {
        if (Objects.isNull(skillVid)) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<IntentDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IntentDO::getSkillVid, skillVid);
        return this.list(wrapper);
    }

    @Override
    public IntentDO queryById(Long id) {
        return this.getById(id);
    }

    @Override
    public List<IntentDO> queryByNameByVidAndNoId(IntentDO intentDO) {
        LambdaQueryWrapper<IntentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IntentDO::getIntentName, intentDO.getIntentName());
        queryWrapper.eq(IntentDO::getSkillVid, intentDO.getSkillVid());
        queryWrapper.ne(IntentDO::getId, intentDO.getId());
        return this.list(queryWrapper);
    }

    @Override
    public boolean updateByIdAndVersion(IntentDO intentDO) {
        LambdaUpdateWrapper<IntentDO> updateWrapper = new LambdaUpdateWrapper<>();
        Integer updateVersion = intentDO.getUpdateVersion();
        updateWrapper.eq(IntentDO::getUpdateVersion, updateVersion);
        updateWrapper.eq(IntentDO::getId, intentDO.getId());
        // 版本自增
        intentDO.setUpdateVersion(updateVersion + 1);
        return this.update(intentDO, updateWrapper);
    }

    @Override
    public List<IntentDO> listBySkillVids(List<Long> skillVids) {
        if (CollectionUtils.isEmpty(skillVids)) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<IntentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(IntentDO::getSkillVid, skillVids);
        return this.list(queryWrapper);
    }

    @Override
    public boolean removeBySkillVids(List<Long> skillVids) {
        if (CollectionUtils.isEmpty(skillVids)) {
            return true;
        }
        LambdaQueryWrapper<IntentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(IntentDO::getSkillVid, skillVids);
        return this.remove(queryWrapper);
    }

    @Override
    public List<IntentDO> listByNames(ArrayList<String> intentNames, Long skillVid) {
        LambdaQueryWrapper<IntentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IntentDO::getSkillVid, skillVid);
        if (CollectionUtils.isEmpty(intentNames)) {
            return Lists.newArrayList();
        }
        queryWrapper.in(IntentDO::getIntentName, intentNames);
        return this.list(queryWrapper);
    }

    @Override
    public List<IntentDO> listByids(ArrayList<Long> intentIds) {
        if (CollectionUtils.isEmpty(intentIds)) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<IntentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(IntentDO::getId, intentIds);
        return this.list(queryWrapper);
    }

    @Override
    public Boolean physicalDeleteBySkillVid(Long skillVid) {
        return this.baseMapper.physicalDeleteBySkillVid(skillVid);
    }
}




